<?php
/* ###############################################################################
 *
 * $Id: sxb.php 61 2015-01-15 18:43:13Z expertsoftware.i@gmail.com $
 *
 * システム名
 *   PHP Web フレームワーク [Ponzu]
 *
 * 機能
 *   sXMLBook ライブラリ
 *
 * バージョン
 *   0.0.0_0
 *
 * ライセンス
 *   LGPL v2.0
 *
 * 履歴
 *   2007/07/01 K.Sonohara
 *     新規作成。
 *
 * ###############################################################################*/


class SXB {
	var $xml;
	var $page = "";
	var $summaryonly = false;

	function SXB($xmlname) {
		$this->xml = xmlFrom($xmlname);
	}


	function xsltBy($xsltname, $options=NULL) {
    if ($options==NULL) {
      $options = array(
        "html.dir"=>SITE_URL,
        "image.dir"=>"images/",
        "item.prefix"=>"0",
        "top.mark"=>"0",
        "title.icon"=>"stop",
        "image.click"=>0,
      );
    };

		$x = xmlFrom($xsltname);

  	return str_replace(
      array(
        ' xmlns=""',
        ' xmlns="http://www.w3.org/1999/xhtml"',
        ' xmlns:xhtml="http://www.w3.org/1999/xhtml"',
      ),
      '',
      xslt($this->xml, $x, $options)
    );
	}

  function printHTML($template="sxb/bootstrap/mikan-item.xsl") {
    $l = PAGE_LEVEL . '/';
    if (PAGE_LEVEL == "-") $l = "";

    $image_click = "0";
    if (defined('SXB_IMAGE_CLICK')) $image_click = SXB_IMAGE_CLICK;

    $indexprefix = "";
    if ($this->summaryonly) {
      $indexprefix = "?page=";
    }

  	//SXB処理
  	printHTML($this->xsltBy(MODULE_HOME . $template, 
      array(
        "html.dir"=>SITE_URL,
        "image.dir"=>"images/". $l,
        "item.prefix"=>"0",
        "top.mark"=>"0",
        "title.icon"=>"book",
        "image.click"=>$image_click,
        "html.index.name"=>$this->page,
        "html.index.prefix"=>$indexprefix,
      )
    ));
  }

  function printMenu($template="sxb/bootstrap/mikan-menu.xsl") {
    $l = PAGE_LEVEL . '/';
    if (PAGE_LEVEL == "-") $l = "";

    $indexprefix = "";
    if ($this->summaryonly) {
      $indexprefix = "?page=";
    }

  	//SXB処理
  	printHTML($this->xsltBy(MODULE_HOME . $template, 
      array(
        "html.index.name"=>$this->page,
        "html.index.prefix"=>$indexprefix,
      )
    ));
  }

	function getInfo($define) {
		$n = nodeFrom($this->xml->documentElement, "/info/" . $define);

    if ($n != null) {
    	return mb_convert_encoding($n->nodeValue, SITE_ENCODE, "UTF-8");
    } else {
      return "";
    }
	}

	function getTitle() {
    if ($this->summaryonly) {
      if ($this->page == "") {
        return $this->getInfo("title");
      }
    }
    return $this->getInfo("title") . " " . $this->getItem($this->page);
	}

	function getDate() {
    return $this->getInfo("date");
	}

	function getCompany() {
    return $this->getInfo("company");
	}

	function getDescription() {
    if ($this->summaryonly) {
      if ($this->page == "") {
        return $this->getInfo("description");
      }
    }
    return $this->getInfo("description") . " " . $this->getItem($this->page);
	}

	function getKeywords() {
     return $this->getInfo("keywords");
	}

	function getItem($define) {
		$n = nodeFrom($this->xml->documentElement, "/item", "id", $define);

    if ($n != null) {
    	return mb_convert_encoding(nodeFrom($n, "title")->nodeValue, SITE_ENCODE, "UTF-8");
    } else {
      return "";
    }
	}
}

function SXB_HTML() {
  global $sxb;

	//SXB処理
  $template = "sxb/bootstrap/mikan-item.xsl";
  if ($sxb->summaryonly) {
    if ($sxb->page == "") {
      $template = "sxb/bootstrap/mikan-summary.xsl";
    } else {
      $template = "sxb/bootstrap/mikan-itemonly.xsl";
    }
  }
	$sxb->printHTML($template);
}

$sxb = null;
function sxbHTML($level=null, $id=null, $summaryonly=false) {
  if (!defined('PAGE_LEVEL'))
    define("PAGE_LEVEL", $level);
  if (!defined('PAGE_ID'))
    define("PAGE_ID",    $id);

  $l = PAGE_LEVEL . '/';
  if (PAGE_LEVEL == "-") $l = "";

  $page = "";
  if ($summaryonly) {
    if (isset($_GET["page"])) {
      $page = $_GET["page"];
    }
  }

  global $sxb;
  $sxb = new SXB(DB_HOME . "sxb/" . $l . PAGE_ID . ".xml");
  $sxb->page = $page;
  $sxb->summaryonly = $summaryonly;
 	define("PAGE_TITLE",  $sxb->getTitle());
	define("PAGE_DATE",  $sxb->getDate());
	define("PAGE_DESCRIPTION",  $sxb->getDescription());
	define("PAGE_KEYWORS",  $sxb->getKeywords());

  if (!defined('PAGE_CAPTION'))
    define("PAGE_CAPTION", true);
  define("PAGE_SIDE", true);
  if (!defined('PAGE_HTML'))
    define("PAGE_HTML", "SXB_HTML");

  if (isset($_GET["bodyonly"])) {
    doHTML(PAGE_HTML);
  } else {
    doHTML();
  }
}

?>
